﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DeviceInfoManage.DataAccessHelper;
using DeviceInfoManage.DataAccessLayer;
/// <summary>
/// RepairInfo 的摘要说明
/// </summary>
/// 
namespace DeviceInfoManage.BusinessLogicLayer
{
    /*此类负责处理与设备维修相关的操作逻辑,它对应数据库中的repairInfo表,该表结构如下*/
    /*
    CREATE TABLE [dbo].[repairInfo] (
	[repairId] [int] IDENTITY (1, 1) NOT NULL ,				                //维修记录编号
    [deviceId] [int] NULL ,							                        //维修设备编号
    [repairPlace] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,           //维修地点
	[repairMan] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,            //维修人
	[repairTime] [float] NULL ,                                             //维修工时
	[repairMoney] [float] NULL ,                                            /维修费用
	[errorReason] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,         //故障原因	
	[repairStartTime] [datetime] NULL ,                                     //维修开始日期
	[repairEndTime] [datetime] NULL ,                                       //维修结束日期
	[repairMemo] [text] COLLATE Chinese_PRC_CI_AS NULL                      //附加信息
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    */
    public class RepairInfo
    {
        private int repairId;/*维修记录编号*/
        private int deviceId;/*设备编号*/
        private string repairPlace; /*设备送修地点*/
        private string repairMan;/*设备维修人*/
        private float repairTime; /*设备维修工时*/
        private float repairMoney;/*设备维修费用*/
        private string errorReason; /*设备故障原因*/
        private DateTime repairStartTime; /*设备送修时间*/
        private DateTime repairEndTime;/*设备返修时间*/
        private int isReturn; /*设备是否已经返修*/
        private string repairMemo;/*附加信息*/
        private string errMessage;/*操作错误信息*/
        /*下面是各个字段对应的属性表示*/
        #region MyRegion
        public int RepairId
        {
            set { this.repairId = value; }
            get { return this.repairId; }
        }
        public int DeviceId
        {
            set { this.deviceId = value; }
            get { return this.deviceId; }
        }
        public string RepairPlace
        {
            set { this.repairPlace = value; }
            get { return this.repairPlace; }
        }
        public string RepairMan
        {
            set { this.repairMan = value; }
            get { return this.repairMan; }
        }
        public float RepairTime
        {
            set { this.repairTime = value; }
            get { return this.repairTime; }
        }
        public float RepairMoney
        {
            set { this.repairMoney = value; }
            get { return this.repairMoney; }
        }
        public string ErrReason
        {
            set { this.errorReason = value; }
            get { return this.errorReason; }
        }
        public DateTime RepairStartTime
        {
            set { this.repairStartTime = value; }
            get { return this.repairStartTime; }
        }
        public DateTime RepairEndTime
        {
            set { this.repairEndTime = value; }
            get { return this.repairEndTime; }
        }
        public int IsReturn
        {
            set { this.isReturn = value; }
            get { return this.isReturn; }
        }
        public string RepairMemo
        {
            set { this.repairMemo = value; }
            get { return this.repairMemo; }
        }
        public string ErrMessage
        {
            set { this.errMessage = value; }
            get { return this.errMessage; }
        }
        #endregion
        
        public RepairInfo()
        {
            repairId = 0;
            deviceId = 0;
            repairPlace = "";
            repairMan = "";
            repairTime = 0.0f;
            repairMoney = 0.0f;
            errorReason = "";
            repairStartTime = DateTime.MinValue;
            repairEndTime = DateTime.MaxValue;
            repairMemo = "";
            errMessage = "";
        }

        /*设备出修:即向系统中加入维修信息记录*/
        public bool AddRepairInfo()
        {
            /*判断是否已经选择了维修的设备*/
            if (this.deviceId == 0)
            {
                this.errMessage = "你没有选择需要维修的设备";
                return false;
            }
            /*判断该设备在系统中是否已经存在*/
            if (!(new DeviceInfo()).IsExist(deviceId))
            {
                this.errMessage = "不存在该设备编号的设备信息";
                return false;
            }
            /*检查设备状态*/
            if ((new DeviceInfo()).GetDeviceState(deviceId) != 1)
            {
                this.errMessage = "该设备未处于空闲状态，不能维修!";
                return false;
            }
            /*通过验证后，可以执行维修信息的添加了*/
            string insertString = "insert into repairInfo(deviceId,repairPlace,repairMan,errorReason,repairStartTime,isReturn,repairMemo) values(";
            insertString += deviceId + ",";
            insertString += SqlString.GetQuotedString(repairPlace) + ",";
            insertString += SqlString.GetQuotedString(repairMan) + ",";
            insertString += SqlString.GetQuotedString(errorReason) + ",'";
            insertString += repairStartTime + "',";
            insertString += isReturn + ",";
            insertString += SqlString.GetQuotedString(repairMemo) + ")";
            /*设备出修后要修改该设备的状态为出修状态,系统中出修状态用数字3表示的*/
            string updateString = "update deviceInfo set deviceState=3 where deviceId=" + deviceId;
            string[] sqlStrings = new string[] { insertString, updateString };
            DataBase db = new DataBase();
            /*调用数据层的ExecuteSQL执行批量的sql产生的事务操作，成功返回true*/
            if (db.ExecuteSQL(sqlStrings) == false)
            {
                this.errMessage = "设备出修进行数据库操作时发生了错误!";
                return false;
            }
            return true;
        }

        /*查询还没有返修的设备出修信息*/
        public DataSet QueryRepairInfo(string deviceName, string deviceTypeName, string startTime, string endTime)
        {
            string queryString = "select * from repairInfoView where deviceName like '%";
            queryString += deviceName + "%' and deviceTypeName like '%" + deviceTypeName + "%'";
            /*如果设置了送修开始时间这个查询条件*/
            if (startTime != "")
                queryString += " and repairStartTime>'" + Convert.ToDateTime(startTime) + "'";
            /*如果设置了送修结束时间这个查询条件*/
            if (endTime != "")
                queryString += " and repairStartTime<'" + Convert.ToDateTime(endTime) + "'";
            queryString += " and isReturn=0";
            /*调用数据层代码*/
            DataBase db = new DataBase();
            return db.GetDataSet(queryString);
        }

        /*查询已经返修了的设备维修历史记录信息*/
        public DataSet QueryRepairHistory(string deviceName, string deviceTypeName, string startTime, string endTime)
        {
            /*此业务逻辑代码被作者屏蔽了,需要的请联系作者购买*/
            return null;
        }
        /*根据维修记录编号得到维修各个字段信息并保存在本类相应字段中*/
        public bool GetRepairInfo(int repairId)
        {
            string queryString = "select * from repairInfo where repairId = " + repairId;
            DataSet ds = (new DataBase()).GetDataSet(queryString);
            if (ds.Tables[0].Rows.Count == 0)
            {
                this.ErrMessage = "不存在该设备维修信息记录";
                return false;
            }
            /*如果查找到该记录，下面用数据库中的各个字段填充本类的对应字段*/
            DataRow dr = ds.Tables[0].Rows[0];
            this.repairId = repairId;
            this.deviceId = Int32.Parse(dr["deviceId"].ToString());
            this.repairPlace = dr["repairPlace"].ToString();
            this.repairMan = dr["repairMan"].ToString();
            if(!dr["repairTime"].Equals(System.DBNull.Value))
              this.repairTime = Convert.ToSingle(dr["repairTime"]);
            if(!dr["repairMoney"].Equals(System.DBNull.Value))
                this.repairMoney = Convert.ToSingle(dr["repairMoney"]);
            this.errorReason = dr["errorReason"].ToString();
            this.repairStartTime = Convert.ToDateTime(dr["repairStartTime"]);
            if(!dr["repairEndTime"].Equals(System.DBNull.Value))
                this.repairEndTime = Convert.ToDateTime(dr["repairEndTime"]);
            this.isReturn = Convert.ToInt16(dr["isReturn"]);
            this.repairMemo = dr["repairMemo"].ToString();

            return true;
        }

        /*设备返修业务逻辑*/
        public bool UpdateRepairInfo()
        {
            /*修改维修信息的sql语句*/
            string repairInfoUpdateString = "update repairInfo set repairMan=";
            repairInfoUpdateString += SqlString.GetQuotedString(repairMan) + ",errorReason=";
            repairInfoUpdateString += SqlString.GetQuotedString(errorReason) + ",repairEndTime='";
            repairInfoUpdateString += repairEndTime + "',repairTime=";
            repairInfoUpdateString += repairTime + ",repairMoney=";
            repairInfoUpdateString += repairMoney + ",repairMemo=";
            repairInfoUpdateString += SqlString.GetQuotedString(repairMemo);
            repairInfoUpdateString += ",isReturn=1 where repairId=" + repairId;
            /*根据维修编号得到维修的设备编号*/
            int deviceId = this.GetDeviceId(repairId);
            /*修改设备状态的sql语句*/
            string deviceStateUpdateString = "update deviceInfo set deviceState = 1 where deviceId = " + deviceId;
            /*将上面两条更新的sql语句组成一个事务操作*/
            string[] updateStrings = new string[] { repairInfoUpdateString, deviceStateUpdateString };
            /*调用数据层进行数据的批处理更新,成功返回true*/
            DataBase db = new DataBase();
            if (!db.ExecuteSQL(updateStrings))
            {
                this.errMessage = "执行设备维修返修操作时发生了错误!";
                return false;
            }
            return true;
        }

        /*根据设备维修记录编号得到维修的设备编号*/
        public int GetDeviceId(int repairId)
        {
            string queryString = "select deviceId from repairInfo where repairId = " + repairId;
            DataSet ds = (new DataBase()).GetDataSet(queryString);
            return Int32.Parse(ds.Tables[0].Rows[0]["deviceId"].ToString());
        }
    }
}

